﻿using Common.Device.Enums;
using Common.Device.Models;
using Common.Device.ViewModels;
using Common.Tool.DataTools;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Common.Device.Views
{
    /// <summary>
    /// TongXinView.xaml 的交互逻辑
    /// </summary>
    public partial class TongXinView : UserControl
    {
        public TongXinViewModel viewModel = new TongXinViewModel();
        public TongXinView(Order order = null)
        {
            InitializeComponent();
            DataContext = viewModel;
            if (order != null)
            {
                //根据测试指令刷新界面
                TongXin parameter = JsonConvert.DeserializeObject<TongXin>(order.deviceModel);
                viewModel.MenuIndex = 0;
                viewModel.DataType = parameter.类型.ToString();
                viewModel.IsNewLine = parameter?.换行符 ?? false;
                viewModel.SendStr = parameter?.发送内容;
                viewModel.WaitTime = parameter?.等待时间 ?? 0;
                viewModel.ReceiveStr = parameter?.接收内容;
            }
        }
        //仪器对象
        public Interfaces.ITongXin Device;
        private void TongXinClick(object sender, RoutedEventArgs e)
        {
            //创建参数
            TongXin parameter = new TongXin()
            {
                类型 = viewModel.DataType,
                换行符 = viewModel.IsNewLine,
                发送内容 = viewModel.SendStr,
                等待时间 = viewModel.WaitTime,
                接收内容 = viewModel.ReceiveStr,
                正则校验 = viewModel.IsRegex
            };
            //输入确认
            if (parameter.发送内容 == null && parameter.接收内容 == null)
            {
                View.Controls.Dialogs.TipView.Show("发送与接收同时为空");
                return;
            }
            if (parameter.发送内容 != null && parameter.类型 == TongXinEnum.DataType.HEX.ToString() 
                && DataConvert.StringToByteArray(parameter.发送内容,false) == null)
            {
                View.Controls.Dialogs.TipView.Show("发送输入有误");
                return;
            }
            if (parameter.接收内容 != null && parameter.类型 == TongXinEnum.DataType.HEX.ToString()
            && DataConvert.StringToByteArray(parameter.接收内容.Replace("xx", "").Replace("XX", ""),false) == null)
            {
                View.Controls.Dialogs.TipView.Show("接收输入有误");
                return;
            }
            if (parameter.正则校验 && parameter.类型 == TongXinEnum.DataType.HEX.ToString())
            {
                View.Controls.Dialogs.TipView.Show("正则校验需为ASCII");
                return;
            }
            if (parameter.接收内容!=null&&parameter.接收内容.ToUpper().StartsWith("XX") && parameter.类型 == TongXinEnum.DataType.HEX.ToString())
            {
                View.Controls.Dialogs.TipView.Show("XX不能作为接收开始字节");
                return;
            }
            if (parameter.接收内容 != null && parameter.类型 == TongXinEnum.DataType.HEX.ToString() && parameter.接收内容.Replace(" ", "").Length < 4)
            {
                View.Controls.Dialogs.TipView.Show("接收长度最少两个字节");
                return;
            }
            if (parameter.发送内容 != null && parameter.接收内容 != null)
            {
                parameter.mode = TongXinEnum.TestMode.发送并接收;
            }
            if (parameter.接收内容 == null)
            {
                parameter.mode = TongXinEnum.TestMode.仅发送;
            }
            if (parameter.发送内容 == null)
            {
                parameter.mode = TongXinEnum.TestMode.仅接收;
            }
            //根据当前串口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder(Device.Name));
            }
        }
    }
}
